package socket.example2;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;

/**
 * @author lhn
 * @date 2024-04-22 14:04
 */
//@SuppressWarnings("all")
public class SocketTCP02Client {
    public static void main(String[] args) throws IOException {
        Socket socket = new Socket(InetAddress.getLocalHost(),9999);
        System.out.println("客户端socket已启动");
        OutputStream outputStream = socket.getOutputStream();
        outputStream.write("hello, server".getBytes());
        socket.shutdownOutput();    //设置写入结束标记

        InputStream inputStream = socket.getInputStream();
        byte[] buf = new byte[1024];
        int readLen = 0;
        while ((readLen = inputStream.read(buf)) !=-1 ) {
            System.out.println(new String(buf, 0, readLen));
        }
        socket.shutdownInput();

        outputStream.close();
        inputStream.close();
        socket.close();
    }
}
